package com.cn.itcast.gjp.controller;

import com.cn.itcast.gjp.domain.Ledger;
import com.cn.itcast.gjp.domain.QueryForm;
import com.cn.itcast.gjp.domain.Sort;
import com.cn.itcast.gjp.mapper.LedgerMapper;
import com.cn.itcast.gjp.mapper.SortMapper;
import com.cn.itcast.gjp.view.AbstractLedgerMngDialog;
import com.mysql.cj.Session;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import java.util.List;

public class LedgerMngController extends AbstractLedgerMngDialog {
    public LedgerMngController(JFrame frame) {
        super(frame);
        refresh();
    }


    //获取金额，总收入以及总支出
    public void getMoney(List<Ledger> ledgers){
        double in=0;
        double pay =0;
        double total=0;

        for (Ledger ledger : ledgers){
            if (ledger.getParent().equals("支出")){
                pay+=ledger.getMoney();
            }
            else {
                in+=ledger.getMoney();
            }
        }

        total=in-pay;

        //将数据赋予页面上
        this.inMoneyTotalLabel.setText("总收入：" + in);
        this.payMoneyTotalLabel.setText("总支出：" + pay);
        this.cha.setText("收支差：" + total);
    }


    private void refresh(){
        //初始化界面
        SqlSession session= MyBatisUtil.getSqlSession();
        LedgerMapper ledgerMapper = session.getMapper(LedgerMapper.class);
        //初始化所有数据
        List<Ledger> ledgers = ledgerMapper.queryAllLedger();
        this.setTableModel(ledgers);

        getMoney(ledgers);

        session.close();
    };

    @Override
    public void addLedger() {
        AddLedgerController addLedgerController = new AddLedgerController(this);
        addLedgerController.setVisible(true);

        this.refresh();
    }

    @Override
    public void editLedger() {
        int row = ledgerDataTable.getSelectedRow();
        if(row<1){
            JOptionPane.showMessageDialog(this, "请选择要编辑的账单！");
            return;
        }
        Ledger ledger = this.getLedgerByTableRow(row);
        if (ledger == null){
            JOptionPane.showMessageDialog(this, "您选择的是空行");
            return;
        }
        EditLedgerController editLedgerController =new EditLedgerController(this,ledger);
        editLedgerController.setVisible(true);
    }

    @Override
    public void deleteLedger() {
        int row = ledgerDataTable.getSelectedRow();
        if(row<1){
            JOptionPane.showMessageDialog(this, "请选择要删除的账单！");
            return;
        }
        else {
            Ledger ledger = this.getLedgerByTableRow(row);
            if (ledger == null){
                JOptionPane.showMessageDialog(this, "您选择的是空行");
                return;
            }
        }
        int result = JOptionPane.showConfirmDialog(this, "您确定要删除账单吗？");
        if (result == 0) {
            SqlSession session= MyBatisUtil.getSqlSession();
            LedgerMapper ledgerMapper = session.getMapper(LedgerMapper.class);
            Ledger ledger = this.getLedgerByTableRow(row);
            ledgerMapper.DeleteLedger(ledger);
            session.commit();
            session.close();
            this.refresh();
        }
    }

    @Override
    public void queryLedger() {
        QueryForm queryForm = new QueryForm();
        queryForm.setBegin(beginDateTxt.getText());
        queryForm.setEnd(endDateTxt.getText());
        queryForm.setParent(parentBox.getSelectedItem().toString());
        queryForm.setSname(sortBox.getSelectedItem().toString());
        //shujuku数据获取
        SqlSession session= MyBatisUtil.getSqlSession();
        LedgerMapper ledgerMapper = session.getMapper(LedgerMapper.class);
        SortMapper sortMapper = session.getMapper(SortMapper.class);
        queryForm.setSid(sortMapper.getSidBySname(queryForm.getSname()));


        if (queryForm.getParent().equals("收入/支出")){
            System.out.println(queryForm.getParent());
            List<Ledger> ledgers = ledgerMapper.queryAllLedgerByForm(queryForm);
            this.setTableModel(ledgers);
            getMoney(ledgers);
            session.close();
        }
        else{
            List<Ledger> ledgers = ledgerMapper.queryLedgerByForm(queryForm);
            this.setTableModel(ledgers);

            getMoney(ledgers);
            session.close();
        }
    }

    @Override
    public void parentChange() {
        SqlSession session= MyBatisUtil.getSqlSession();
        SortMapper sortMapper = session.getMapper(SortMapper.class);

        String parent = parentBox.getSelectedItem().toString();
        if (parent.equals("-请选择-")){
            Object[] sortItems = {"-请选择-"};
            sortBox.setModel(new DefaultComboBoxModel(sortItems));
        }
        else if (parent.equals("收入/支出") ) {
            // 情况二, “收入/支出”
            List<String> sortItems = sortMapper.getAllSname();
            sortItems.add(0, "-请选择-");
            sortBox.setModel(new DefaultComboBoxModel(sortItems.toArray() ));
        }
        else {
            // 情况三, “收入”或“支出”
            List<String> sortItems = sortMapper.getSnameByParent(parent);
            sortItems.add(0, "-请选择-");
            sortBox.setModel(new DefaultComboBoxModel(sortItems.toArray()));
        }
    }
}
